package com.lswms.modules.lswms.controller.check;

import com.alibaba.druid.util.StringUtils;
import com.lswms.common.annotation.LogOperation;
import com.lswms.common.constant.Constant;
import com.lswms.common.page.PageData;
import com.lswms.common.utils.ExcelUtils;
import com.lswms.common.utils.Result;
import com.lswms.common.validator.AssertUtils;
import com.lswms.common.validator.ValidatorUtils;
import com.lswms.common.validator.group.AddGroup;
import com.lswms.common.validator.group.DefaultGroup;
import com.lswms.common.validator.group.UpdateGroup;
import com.lswms.modules.lswms.MEGConstant;
import com.lswms.modules.lswms.dto.check.CheckDTO;
import com.lswms.modules.lswms.dto.orders.OrderdetailDTO;
import com.lswms.modules.lswms.entity.check.CheckEntity;
import com.lswms.modules.lswms.entity.orders.OrderdetailEntity;
import com.lswms.modules.lswms.excel.check.CheckExcel;
import com.lswms.modules.lswms.service.check.CheckService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 盘点
 *
 * @author DX
 * @since 1.0.0 2021-07-08
 */
@RestController
@RequestMapping("lswms/check")
@Api(tags = "盘点")
public class CheckController {
	@Autowired
	private CheckService checkService;

	@GetMapping("page")
	@ApiOperation("分页")
	@ApiImplicitParams({
			@ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType = "int"),
			@ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query", required = true, dataType = "int"),
			@ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType = "String"),
			@ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType = "String") })
//	@RequiresPermissions("lswms:check:page")
	public Result<PageData<CheckDTO>> page(@ApiIgnore @RequestParam Map<String, Object> params) {
		PageData<CheckDTO> page = checkService.page(params);

		return new Result<PageData<CheckDTO>>().ok(page);
	}

	@GetMapping("{id}")
	@ApiOperation("信息")
//	@RequiresPermissions("lswms:check:info")
	public Result<CheckDTO> get(@PathVariable("id") Long id) {
		CheckDTO data = checkService.get(id);

		return new Result<CheckDTO>().ok(data);
	}

	@PostMapping
	@ApiOperation("保存")
	@LogOperation("保存")
	@RequiresPermissions("lswms:check:save")
	public Result save(@RequestBody CheckDTO dto) {
		// 效验数据
		ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);

		checkService.save(dto);

		return new Result();
	}

	@PutMapping
	@ApiOperation("修改")
	@LogOperation("修改")
	@RequiresPermissions("lswms:check:update")
	public Result update(@RequestBody CheckDTO dto) {
		// 效验数据
		try {
			ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);
			String msg = checkService.valideAndUpdate(dto);

			if (StringUtils.isEmpty(msg)) {
				return new Result();
			} else {
				return new Result().error(msg);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return new Result().error(MEGConstant.IS_ERROR);
		}
	}

	@DeleteMapping
	@ApiOperation("删除")
	@LogOperation("删除")
	@RequiresPermissions("lswms:check:delete")
	public Result delete(@RequestBody Long[] ids) {

		try {
			AssertUtils.isArrayEmpty(ids, "id");
			String msg = checkService.valideAndDelete(ids);

			if (StringUtils.isEmpty(msg)) {
				return new Result();
			} else {
				return new Result().error(msg);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return new Result().error(MEGConstant.IS_ERROR);
		}
	}

	@GetMapping("export")
	@ApiOperation("导出")
	@LogOperation("导出")
	@RequiresPermissions("lswms:check:export")
	public void export(@ApiIgnore @RequestParam Map<String, Object> params, HttpServletResponse response)
			throws Exception {
		List<CheckDTO> list = checkService.list(params);

		ExcelUtils.exportExcelToTarget(response, null, list, CheckExcel.class);
	}

	@PutMapping("createcheck")
	@ApiOperation("生成盘点明细")
	@RequiresPermissions("lswms:check:createcheck")
	public Result createcheck(@RequestBody List<CheckEntity> list) {

		try {
			// Object orders =map.get("orders"); //request.getParameter("orders");
			// List<OrderdetailEntity> orderList = JSON.parseArray(orders,
			// OrderdetailEntity.class);

			// ConvertUtils.sourceToTarget(orders, OrderdetailEntity.class);
			if (list.size() == 0) {
				return new Result().error("请选择盘点单");
			} else if (list.size() > 1) {
				return new Result().error("只能选择一条记录");
			}

			CheckEntity checkEntity = list.get(0);
			String msg = checkService.createcheck(checkEntity);

			if (StringUtils.isEmpty(msg)) {
				return new Result();
			} else {
				return new Result().error(msg);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return new Result().error(MEGConstant.IS_ERROR);
		}
	}

	@PutMapping("createcheckTask")
	@ApiOperation("生成盘点任务")
	@RequiresPermissions("lswms:check:createcheckTask")
	public Result createcheckTask(@RequestBody List<CheckEntity> list) {

		try {
			// Object orders =map.get("orders"); //request.getParameter("orders");
			// List<OrderdetailEntity> orderList = JSON.parseArray(orders,
			// OrderdetailEntity.class);

			// ConvertUtils.sourceToTarget(orders, OrderdetailEntity.class);
			if (list.size() == 0) {
				return new Result().error("请选择盘点单");
			} else if (list.size() > 1) {
				return new Result().error("只能选择一条记录");
			}

			CheckEntity checkEntity = list.get(0);
			if (checkEntity.getFromloc().equals("PY")){
				return new Result().error("PY无需生成盘点任务");
			}
			String msg = checkService.createcheckTask(checkEntity);

			if (StringUtils.isEmpty(msg)) {
				return new Result();
			} else {
				return new Result().error(msg);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return new Result().error(MEGConstant.IS_ERROR);
		}
	}

	@PutMapping("CheckPost")
	@ApiOperation("盘点过账")
	@RequiresPermissions("lswms:check:CheckPost")
	public Result checkPost(@RequestBody List<CheckEntity> list) {

		try {
			// Object orders =map.get("orders"); //request.getParameter("orders");
			// List<OrderdetailEntity> orderList = JSON.parseArray(orders,
			// OrderdetailEntity.class);

			// ConvertUtils.sourceToTarget(orders, OrderdetailEntity.class);
			if (list.size() == 0) {
				return new Result().error("请选择盘点单");
			} else if (list.size() > 1) {
				return new Result().error("只能选择一条记录");
			}

			CheckEntity checkEntity = list.get(0);
			String msg = checkService.checkPost(checkEntity);

			if (StringUtils.isEmpty(msg)) {
				return new Result();
			} else {
				return new Result().error(msg);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return new Result().error(MEGConstant.IS_ERROR);
		}
	}

	@PutMapping("cancelCheckPost")
	@ApiOperation("取消盘点")
	@RequiresPermissions("lswms:check:cancelCheckPost")
	public Result cancelCheckPost(@RequestBody List<CheckEntity> list) {
		try {
			// Object orders =map.get("orders"); //request.getParameter("orders");
			// List<OrderdetailEntity> orderList = JSON.parseArray(orders,
			// OrderdetailEntity.class);

			// ConvertUtils.sourceToTarget(orders, OrderdetailEntity.class);
			if (list.size() == 0) {
				return new Result().error("请选择盘点单");
			} else if (list.size() > 1) {
				return new Result().error("只能选择一条记录");
			}

			CheckEntity checkEntity = list.get(0);
			String msg = checkService.cancelCheckPost(checkEntity);

			if (StringUtils.isEmpty(msg)) {
				return new Result();
			} else {
				return new Result().error(msg);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return new Result().error(MEGConstant.IS_ERROR);
		}
	}

}